/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.stu.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.stu.entity.OtStudent;
import com.thinkgem.jeesite.modules.stu.service.OtStudentService;

/**
 * 学生管理Controller
 * @author hu
 * @version 2017-09-29
 */
@Controller
@RequestMapping(value = "${adminPath}/stu/otStudent")
public class OtStudentController extends BaseController {

	@Autowired
	private OtStudentService otStudentService;
	
	@ModelAttribute
	public OtStudent get(@RequestParam(required=false) String id) {
		OtStudent entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = otStudentService.get(id);
		}
		if (entity == null){
			entity = new OtStudent();
		}
		return entity;
	}
	
	@RequiresPermissions("stu:otStudent:view")
	@RequestMapping(value = {"list", ""})
	public String list(OtStudent otStudent, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<OtStudent> page = otStudentService.findPage(new Page<OtStudent>(request, response), otStudent); 
		model.addAttribute("page", page);
		return "modules/stu/otStudentList";
	}

	@RequiresPermissions("stu:otStudent:view")
	@RequestMapping(value = "form")
	public String form(OtStudent otStudent, Model model) {
		model.addAttribute("otStudent", otStudent);
		return "modules/stu/otStudentForm";
	}

	@RequiresPermissions("stu:otStudent:edit")
	@RequestMapping(value = "save")
	public String save(OtStudent otStudent, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, otStudent)){
			return form(otStudent, model);
		}
		otStudentService.save(otStudent);
		addMessage(redirectAttributes, "保存学生信息成功");
		return "redirect:"+Global.getAdminPath()+"/stu/otStudent/?repage";
	}
	
	@RequiresPermissions("stu:otStudent:edit")
	@RequestMapping(value = "delete")
	public String delete(OtStudent otStudent, RedirectAttributes redirectAttributes) {
		otStudentService.delete(otStudent);
		addMessage(redirectAttributes, "删除学生信息成功");
		return "redirect:"+Global.getAdminPath()+"/stu/otStudent/?repage";
	}

}